package algotithm.jianzhioffer.test53;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/4-16:34
 * @Title 在排序数组中查找数字 I
 */

public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[] nums = {5,7,7,8,8,10};
        int target = 8;
        int ans = solution.search(nums,target);
        System.out.println(ans);
    }

    public int search(int[] nums, int target) {
        return helper(nums,target) - helper(nums,target-1);
    }
    public int helper(int[] nums,int target) {
        int i = 0,j = nums.length - 1;
        while(i <= j) {
            int mid = i + (j - i) / 2;
            if (target < nums[mid]) {
                j = mid - 1;
            } else {
                i = mid + 1;
            }
        }
        return i;
    }
}

